(function($) {
	$('.ya-tooltip').tooltip();
	// fix accordion heading state
	$('.accordion-heading').each(function(){
		var $this = $(this), $body = $this.siblings('.accordion-body');
		if (!$body.hasClass('in')){
			$this.find('.accordion-toggle').addClass('collapsed');
		}
	});
	
	// twice click
	$(document).on('click.twice', '.open [data-toggle="dropdown"]', function(e){
		var $this = $(this), href = $this.attr('href');
		e.preventDefault();
		window.location.href = href;
		return false;
	});
	
    $('#cpanel').collapse();
	
    $('#cpanel-reset').on('click', function(e) {

    	if (document.cookie && document.cookie != '') {
    		var split = document.cookie.split(';');
    		for (var i = 0; i < split.length; i++) {
    			var name_value = split[i].split("=");
    			name_value[0] = name_value[0].replace(/^ /, '');
    			
    			if (name_value[0].indexOf(cpanel_name)===0) {
    				$.cookie(name_value[0], 1, { path: '/', expires: -1 });
    			}
    		}
    	}
    	
    	location.reload();
    });
    
	$('#cpanel-form').on('submit', function(e){
		var $this = $(this), data = $this.data(), values = $this.serializeArray();
		
		var checkbox = $this.find('input:checkbox');
		$.each(checkbox, function() {
			
			if( !$(this).is(':checked') ) {
				name = $(this).attr('name');
				name = name.replace(/([^\[]*)\[(.*)\]/g, '$1_$2');
				
				$.cookie( name , 0, { path: '/', expires: 7 });
			} 
			
		})
		
		$.each(values, function(){
			var $nvp = this;
			var name = $nvp.name;
			var value = $nvp.value;
			
			if ( !(name.indexOf(cpanel_name + '[')===0) ) return ;
			
			//console.log('name: ' + name + ' -> value: ' +value);
			name = name.replace(/([^\[]*)\[(.*)\]/g, '$1_$2');
			
			$.cookie( name , value, { path: '/', expires: 7 });
			 
		});
		
		location.reload();
		
		return false;
		
	});
	
	$('a[href="#cpanel-form"]').on( 'click', function(e) {
		var parent = $('#cpanel-form'), right = parent.css('right'), width = parent.width();
		
		if ( parseFloat(right) < -10 ) {
			parent.animate({
				right: '0px',
			}, "slow");
		} else {
			parent.animate({
				right: '-' + width ,
			}, "slow");
		}
		
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
		} else $(this).addClass('active');
		
		e.preventDefault();
	});
	
$(window).on("load resize",function(){
	$('.nav-mega').each(function(){
		var $menu = $(this), $menu_dim = {
			left: $menu.offset().left,
			width: $menu.width()
		};
		//console.log( $menu );
		//console.log( $menu_dim );
						
		$('.dropdown', $menu).each(function(){
			var $parent_dim = {
				left: $(this).offset().left,
				width: $(this).outerWidth(true)
			};
			//console.log( $(this) );
			//console.log( $parent_dim );
						
			$dropdown = $('.dropdown-menu', this);
			if ( $dropdown.length ){
				var $drop_dim = {
					left: $dropdown.offset().left,
					width: $dropdown.outerWidth(true)
				}
				if ( $drop_dim.width > $menu_dim.width ){
					$drop_dim_left = $menu_dim.left - $parent_dim.left - ($drop_dim.width-$menu_dim.width)/2;
					$dropdown.css({left: $drop_dim_left});
				} else {
					var overright = overleft = false;
					// over right
					if ( $parent_dim.left + $drop_dim.left + $drop_dim.width > $menu_dim.left + $menu_dim.width){
						var $drop_dim_by_right = - $parent_dim.left - $drop_dim.width + $menu_dim.left + $menu_dim.width;
						overright = true;
					} else if ( $parent_dim.left + $parent_dim.width - $drop_dim.width < $menu_dim.left ){
						var $drop_dim_by_left = $menu_dim.left - $parent_dim.left;
						overleft = true;
					}
					//console.log( $dropdown );
					//console.log( $drop_dim );
					//overleft && console.log('->toleft: '+ $drop_dim_by_right );
					//overright && console.log('->toright: '+ $drop_dim_by_right );
					//console.log('------------------------------------------------------------------');
					if ( overright ){						
						$dropdown.css({left: $drop_dim_by_right});
					} else if ( overleft ){
						$dropdown.css({left: $drop_dim_by_left});
					}
				}
			}
		});
		// $menu.on('hover');
		// console.log( $menu_dim );
	});
});
	$(window).on("load resize",function() {
		$('.yaslider').each(function(){
			var height = $("#"+this.id).height();
			$("#"+this.id + ' .carousel-control').css('top', height/2);
		});
	});
	jQuery('.group').each(function(){
		$("#"+(this.id)).colorbox();
	});
	
	(function() {
		function MenuResize(){
		   var modclass_cssmenu = ".menu-header ul.nav > li.active > a:after";
			var width_li = $(".menu-header ul.nav > li.active > a").innerWidth()/2 ;
			$("<style type='text/css'>" + modclass_cssmenu + "{ border-left-width:"+ (parseInt(width_li)) + "px;border-top-width: 12px; border-bottom-width: 0px; border-style: solid; border-right-width:"+ width_li +"px;} </style>").appendTo("head");
		}
		$(window).on("resize", MenuResize);
		$(document).on("ready", MenuResize);
	})();
	/*(function() {
		function MenuResize(){
		   var modclass_cssmenu = "#megaMenu ul.megaMenu > li.current-menu-ancestor:after";
			var width_li = $("#megaMenu ul.megaMenu > li.current-menu-ancestor").innerWidth()/2 ;
			$("<style type='text/css'>" + modclass_cssmenu + "{ border-left-width:"+ (parseInt(width_li)) + "px;border-top-width: 12px; border-bottom-width: 0px; border-style: solid; border-right-width:"+ width_li +"px;} </style>").appendTo("head");
		}
		$(window).on("resize", MenuResizeMega);
		$(document).on("ready", MenuResizeMega);
	})();*/
	(function() {
		function MenuResizeMega(){
		   var modclass_cssmenu = "#megaMenu ul.megaMenu > li.current-menu-item >a:after, #megaMenu ul.megaMenu > li.current-menu-parent >a:after, #megaMenu ul.megaMenu > li.current-menu-ancestor >a:after";
			var width_li = $("#megaMenu ul.megaMenu > li.current-menu-item >a, #megaMenu ul.megaMenu > li.current-menu-parent >a, #megaMenu ul.megaMenu > li.current-menu-ancestor >a").innerWidth()/2 ;
			$("<style type='text/css'>" + modclass_cssmenu + "{ border-left-width:"+ (parseInt(width_li)) + "px;border-top-width: 12px; border-bottom-width: 0px; border-style: solid; border-right-width:"+ width_li +"px;} </style>").appendTo("head");
		}
		$(window).on("resize", MenuResizeMega);
		$(document).on("ready", MenuResizeMega);
	})();
	jQuery(function($){
	// back to top
	$("#ya-totop").hide();
	$(function () {
		var wh = $(window).height();
		var whtml = $(document).height();
		$(window).scroll(function () {
			if ($(this).scrollTop() > whtml/10) {
					$('#ya-totop').fadeIn();
				} else {
					$('#ya-totop').fadeOut();
				}
			});
		$('#ya-totop').click(function () {
			$('body,html').animate({
				scrollTop: 0
			}, 800);
			return false;
			});
	});
	// end back to top
	}); 
}(jQuery));

	
	
	
	
	